package com.ricky.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Element;

import com.ricky.config.Unit.BuildingUnit;
import com.ricky.config.Unit.GoodsUnit;
import com.ricky.config.Unit.HeroUnit;
import com.ricky.config.scene.MapConfig;
import com.ricky.config.scene.SceneConfig;
import com.ricky.core.net.ByteBuffer;
import com.ricky.utils.ConfigHelper;
import com.ricky.utils.FileUtils;

public class ConfigLoader {
	
	protected static Logger log = Logger.getLogger(ConfigLoader.class);

	private static final String rootDir = System.getProperty("user.dir");
	private static final String serverInfoPath = rootDir + "/config/game/server.xml";
	
	private static final String heroUnitPath = rootDir + "/config/unit/HeroUnit.txt";
	private static final String buildingUnitPath = rootDir + "/config/unit/BuildingUnit.txt";
	private static final String goddsUnitPath = rootDir + "/config/unit/GoodsUnit.txt";
	
	private static final String scenePath=rootDir + "/config/scene/scene.xml";

	public static void load() 
	{
		loadServerInfo();
		LoadLan();
		
		loadUnit();
		loadScene();
	}
	
	private static void loadServerInfo()
	{
		Element root=FileUtils.readXMLFile(serverInfoPath);
		Element server=root.element("com.ricky.config.ServerInfo");
		ConfigData.serverinfo=new ServerInfo();
		ConfigData.serverinfo.setAppId(Integer.parseInt(server.elementText("appId")));
		ConfigData.serverinfo.setPort(Integer.parseInt(server.elementText("port")));
		ConfigData.serverinfo.setDebug(Boolean.parseBoolean(server.elementText("appId")));
		ConfigData.serverinfo.setAppName(server.elementText("appName"));
		ConfigData.serverinfo.setLanguage(server.elementText("language"));
		ConfigData.serverinfo.setMongoDB(server.elementText("mongoDB"));
		log.info("服务器配置信息加载完成！"+ConfigData.serverinfo);
	}
	
	private static void LoadLan()
	{
		String lanPath=rootDir+ConfigData.serverinfo.getLanguage();		
		try {
			Lan.load(new FileInputStream(new File(lanPath)));
		} catch (FileNotFoundException e) {
			log.error(e.getMessage(),e);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		log.info("语言包加载完成！");
	}
	
	/**
	 * 加载单位配置数据
	 */
	private static void loadUnit()
	{
		String heroData=FileUtils.readUTF8File(heroUnitPath);
		Map<String,HeroUnit> heros=ConfigHelper.decompressionMap(heroData, "unitID",HeroUnit.class);
		ConfigData.setHeroUnitConfig(heros);
		
		String buildingData=FileUtils.readUTF8File(buildingUnitPath);
		Map<String,BuildingUnit> building=ConfigHelper.decompressionMap(buildingData,"unitID", BuildingUnit.class);
		ConfigData.setBuildingUnitConfig(building);
		
		String goodsData=FileUtils.readUTF8File(goddsUnitPath);
		Map<String,GoodsUnit> goods=ConfigHelper.decompressionMap(goodsData,"unitID", GoodsUnit.class);
		ConfigData.setGoodsUnitConfig(goods);
		
		log.info("单位配置数据加载完成！");

	}
	
	private static void loadScene()
	{
		Element root =FileUtils.readXMLFile(scenePath);
		Map<String,SceneConfig> sc=ConfigHelper.decompression(root,"sceneID",SceneConfig.class);
		ConfigData.setSceneConfig(sc);
		log.info("场景配置数据加载完成！"+sc.size());
		
		String map="";
		SceneConfig scf=null;
		ByteBuffer bbf=null;
		for(String key : sc.keySet())
		{
			scf=sc.get(key);
			map=rootDir+scf.getMapURL();
			bbf=FileUtils.readFileToByteBuffer(map);
			scf.setMap(MapConfig.decoder(bbf));
			log.info("场景\""+scf.getSceneName()+"\"阻挡数据加载完成！"+scf.getMap().name);
		}
	}
}
